﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kassasysteem_TacoTruijens
{
    public partial class pickseats : System.Web.UI.Page
    {
        private DataClassesDataContext Database = new DataClassesDataContext();
        private Voorstelling selectedShow;
        private int adultCounter, childCounter, fiftyPlusCounter, cjpStudentCounter, drieDGlassesCounter;

        protected void Page_Load(object sender, EventArgs e)
        {
            initSelectedShow();
            initCounters();
            initSeats();
            addPreviousNextButtons();
        }

        private void addPreviousNextButtons()
        {
            Literal literal = new Literal();
            literal.Text = "<br /><table class='previousNext'><tr><td class='left'><input type='button' onclick='previous()' value='Terug' /></td><td class='right'><input type='button' onclick='next()' value='Volgende' /></td></tr></table>";
            panelSeats.Controls.Add(literal);
        }

        private void initSeats()
        {
            var tickets = from t in Database.Tickets
                          where t.Voorstelling == selectedShow
                          select t;

            List<Stoel> occupiedSeats = new List<Stoel>();

            foreach (Ticket t in tickets)
            {
                occupiedSeats.Add(t.Stoel);
            }

            var rows = from r in Database.Rijs
                       where r.Zaal == selectedShow.Zaal
                       select r;

            foreach (Rij r in rows)
            {
                Panel panelRow = new Panel();
                Label labelRow = new Label();
                labelRow.Text = "<b>" + r.nummer.ToString() + "</b>";
                panelRow.Controls.Add(labelRow);

                var seats = from s in Database.Stoels
                             where s.Rij == r
                             select s;

                foreach (Stoel s in seats)
                {
                    String text = "<input type='button' id='row" + s.Rij.nummer.ToString() + "seat" + s.nummer.ToString() + "' onclick='seatOnClick(" + s.Rij.nummer.ToString() + "," + s.nummer.ToString() + ")' value='" + s.nummer.ToString() + "' ";

                    if (occupiedSeats.Contains(s))
                    {
                        text += "disabled='disabled' class='occupied' />";
                    }
                    else
                    {
                        text += "class='free' />";
                    }

                    Literal literalSeat = new Literal();
                    literalSeat.Text = text;
                    panelRow.Controls.Add(literalSeat);
                }

                panelSeats.Controls.Add(panelRow);
            }
        }

        private void initSelectedShow()
        {
            DateTime datetime = DateTime.Parse(Request.QueryString["datetime"]);
            int roomNumber = Convert.ToInt32(Request.QueryString["roomnumber"]);
            String movieName = Request.QueryString["moviename"];

            var shows = from s in Database.Voorstellings
                        where s.datumtijd.Equals(datetime) && s.Zaal.nummer == roomNumber && s.Film.naam.Replace((char)39, ' ').Equals(movieName)
                        select s;

            selectedShow = shows.First();
        }

        private void initCounters()
        {
            adultCounter = Convert.ToInt32(Request.QueryString["adultcounter"]);
            childCounter = Convert.ToInt32(Request.QueryString["childcounter"]);
            fiftyPlusCounter = Convert.ToInt32(Request.QueryString["50pluscounter"]);
            cjpStudentCounter = Convert.ToInt32(Request.QueryString["cjpstudentcounter"]);
            drieDGlassesCounter = Convert.ToInt32(Request.QueryString["3dglassescounter"]);
        }
    }
}